<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="选择店铺：" prop="storeId">
        <el-select v-model="queryParams.storeId" placeholder="请选择店铺" filterable>
          <el-option v-for="(item,index) in storeList" :key="index"
                     :label="item.storeUserName + ' - ' + item.storeName"
                     :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="时间范围">
        <el-date-picker
          v-model="dateTime"
          type="datetimerange"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd HH:mm:ss"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storeUserRelateList" @selection-change="handleSelectionChange"
              :tree-props="{children: 'storeGradRelateList'}" row-key="id" border stripe
              :header-cell-class-name="addClass">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="店铺账号" prop="storeUserName" align="center"></el-table-column>
      <el-table-column label="用户名" prop="userName" align="center"></el-table-column>
      <el-table-column label="店铺名称" prop="storeName" align="center"></el-table-column>
      <el-table-column label="总利润" prop="profit" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="goOrderCommission(scope.row,'1')"> {{ scope.row.profit }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="提成比例" prop="gradRate" align="center"></el-table-column>
      <el-table-column label="提成金额" prop="commission" align="center"></el-table-column>
      <el-table-column label="提成时间" prop="commissionTime" align="center"></el-table-column>
      <el-table-column label="退款金额" prop="refundAmount" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="goOrderCommission(scope.row,'2')"> {{ scope.row.refundAmount }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="广告费用" prop="advertisementAmount" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="goAdvertisementLog(scope.row)">
            {{ scope.row.advertisementAmount }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="测评费用" prop="evaluationAmount" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="goEvaluationOrderAmount(scope.row)">
            {{ scope.row.evaluationAmount }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="实际可得" prop="realCommission" align="center"></el-table-column>
      <el-table-column label="未统计订单" prop="notCommissionOrderNum" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="goOrderCommission(scope.row,'0')">
            {{ scope.row.notCommissionOrderNum }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="是否确认" prop="isConfirm" align="center">
        <template slot-scope="scope">
          <div>{{ scope.row.isConfirm == 0 ? '未确认' : '已确认' }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <div v-if="scope.row.isConfirm == '0' ">
            <el-button size="mini" type="text" :loading="recalculateLoading" @click="recalculate(scope.row)">重新计算
            </el-button>
            <el-button size="mini" type="text" @click="confirmCommissionData(scope.row)">
              确定
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  queryShopCommissionInfoList, getStoreList, restTrial, confirmCommission
} from "@/api/commission/commission";
import {getStoreInfo} from "@/api/order/orderApi";

export default {
  name: "StoreUserRelate",
  dicts: ['erp_product_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 提成信息列表
      storeUserRelateList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        storeId: undefined,
      },
      dateTime: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      storeList: [],
      recalculateLoading: false,//重新计算按钮的load加载
    };
  },
  created() {
    this.getList();
    this.getStoreList()
  },
  methods: {
    // 设置表头颜色
    addClass({row, rowIndex, column, columnIndex}) {
      if (columnIndex == 4 || columnIndex == 6 || columnIndex == 11) {
        return "redText"
      }
    },
    // 确定订单提成数据
    confirmCommissionData(row) {
      this.$confirm('确定后将无法修改，请确认该条数据无误？', '提示').then(() => {
        confirmCommission({
          id: row.id
        }).then(res => {
          this.$message.success(res.msg)
          this.getList()
        });
      });
    },
    // 跳转广告投放页面
    goAdvertisementLog(row) {
      this.$confirm('是否前往广告投放记录页面？', '提示').then(() => {
        let queryData = {
          storeId: row.storeId,
          searchTime: row.commissionTime,
        }
        this.$router.push({
          path: "../advertisement/advertisementLog",
          query: {
            queryData: JSON.stringify(queryData)
          }
        })
      })
    },
    // 跳转测评费用页面
    goEvaluationOrderAmount(row) {
      this.$confirm('是否确定前往测评金额页面？', '提示').then(() => {
        let queryData = {
          sellerLoginId: row.storeUserName,
          searchTime: row.commissionTime,
        }
        this.$router.push({
          path: "../evaluation/orderAmount",
          query: {
            queryData: JSON.stringify(queryData)
          }
        })
      })
    },
    // 跳转订单提成页面
    goOrderCommission(row, type) {
      this.$confirm('是否前往订单提成页面查看数据？', '提示').then(() => {
        let queryData = {
          storeId: row.storeId,
          commissionFlag: type,
          searchTime: row.commissionTime,
          userId: row.userId
        }
        this.$router.push({
          path: "./orderCommission",
          query: {
            queryData: JSON.stringify(queryData)
          }
        })
      })
    },
    // 重新计算
    recalculate(row) {
      this.$confirm('是否确定重新计算？', '提示').then(() => {
        this.recalculateLoading = true
        let data = {
          storeId: row.storeId,
          storeUserName: row.storeUserName,
          userId: row.userId,
          commissionTime: row.commissionTime,
        }
        restTrial(data).then(res => {
          this.$message.success(res.msg)
          this.getList()
          this.recalculateLoading = false;
        })
      });
    },
    //  获取店铺列表
    getStoreList() {
      this.loading = true
      getStoreInfo().then(response => {
        this.storeList = response
      })
    },
    // // 获取店铺账号选择列表
    // getStoreList() {
    //   getStoreList().then(res => {
    //     this.storeList = res.data.empowerList;
    //   });
    // },
    /** 查询用户店铺维护关联列表 */
    getList() {
      this.loading = true;
      // console.log('&&&&', this.queryParams)
      this.queryParams.startTime = this.dateTime[0]
      this.queryParams.endTime = this.dateTime[1]
      queryShopCommissionInfoList(this.queryParams).then(response => {
        this.storeUserRelateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        storeGradRelateList: [{
          'aeProductId': '',
          "gradRate": '',
          'type': '1'
        }],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
  }
};
</script>
<style lang="scss" scoped>
::v-deep .el-table__header-wrapper th.redText {
  color: #ff0000;
}
</style>
